package com.chen.mobiletwo.controller;

import com.chen.mobiletwo.entity.ResultEntity;
import com.chen.mobiletwo.service.MessageService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Delete;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("v2/Message/")
public class MessageController {
    private static final Logger logger= LoggerFactory.getLogger(MessageController.class);//日志getLogger需要传入的类名
    @Resource
    private RedisTemplate<String ,String> redisTemplate;//Redis工具类

    @Resource
    MessageService messageService;//注入

    /**
     * 返回信息列表
     * @return Json
     */
    @GetMapping("getMessageList")
    private String getMessageList(){
        return messageService.getMessageList();
    }

    /**
     * 根据id获取消息对象
     * @param messageId 消息id
     * @return String
     */
    @GetMapping("getMessageById")
    private String getMessageById(@RequestParam(value = "messageId",defaultValue = "-1") int messageId){
        if (messageId==-1){
            return ResultEntity.fail(-10002);
        }
        return messageService.getMessageById(messageId);
    }

    /**
     * 根据消息类型获取消息列表
     * @param messageType 消息类型
     * @return String
     */
    @GetMapping("getMessageByType")
    private String getMessageByType(@RequestParam(value = "messageType",defaultValue = "") String messageType){
        if (messageType.equals("")){
            return ResultEntity.fail(-10002);
        }
        return messageService.getMessageByType(messageType);
    }

    /**
     * 根据收件人获取消息列表
     * @param messageRec 收件人
     * @return String
     */
    @GetMapping("getMessageByRec")
    private String getMessageByRec(@RequestParam(value = "messageRec",defaultValue = "") String messageRec){
        if (messageRec.equals("")){
            return ResultEntity.fail(-10002);
        }
        return messageService.getMessageByRec(messageRec);
    }

    /**
     * 根据id删除
     * @param messageId id
     * @return String
     */
    @DeleteMapping("Message")
    private String delMessage(@RequestParam(value = "messageId",defaultValue = "-1") int messageId){
        if (messageId==-1){
            return ResultEntity.fail(-10002);
        }
        return messageService.delMessage(messageId);
    }

    /**
     *  重发邮件
     * @param messageId id
     * @return json
     */
    @PostMapping("rePushMail")
    private String rePushMail(@RequestParam(value = "messageId",defaultValue = "-1") int messageId){
        if (messageId==-1){
            return ResultEntity.fail(-10002);
        }
        return messageService.rePushMail(messageId);
    }
}
